<?php

class Action_Quest_AdminQuest extends RL_Handler{
    private $view;
    private $oQuest;
    /**
	* @var Model_Quest
	**/
	private $model;

    public function init() {
        $this->rh->useClass("Model_Item");
        $this->model = $this->ctx->mm->getModel("Quest");
        $this->oQuest = $this->model->getItem($this->params['quest_id']);
        //проверим, можно ли юзеру быть модератором
        $roles = $this->model->getUserRole($this->oQuest);
        if(!$roles[Model_Quest::ROLE_GIVER])
            die("Access denited");

        if(isset($this->rh->POST['update'])) {
            $this->update();
        }
    }

    public function process() {
 		// -- old theme HACK : 2008-05-21 : lucky -- {{{
		$community_skin = 'platonica';
		$tpl = $this->ctx->tpl;
		$community_model = $this->ctx->mm->getModel("Community");
		$community_model->setDefaultSkin($community_skin);
		$tpl->setSkin($community_skin);
		$views_php = $tpl->findFile('', 'views', $tpl->_skin, 'php');
		unset ($config);
		include $views_php; // got $config
		$this->ctx->rh->views->setViewConfig($config);
		// -- old theme HACK : 2008-05-21 : lucky -- }}}

        $this->view = $this->rh->views->findByName('admin_quest');
        //получаем все документы сообщества
        $aclData = $this->module->aa->getInfo();
        $communityId = $aclData['comity_oid'];
        $sql = "SELECT a.oid, a.title, q.state
            FROM ".$this->ctx->db->db_prefix."class_allfeed a
            INNER JOIN ".$this->ctx->db->db_prefix."objects o
                USING(oid)
            LEFT JOIN ".$this->ctx->db->db_prefix."class_quest_object q
                ON q.quest_oid=".$this->oQuest->id." AND q.object_oid=a.oid
            WHERE a.community_oid=".$communityId." AND a.is_deleted=0
            AND o.type<>'fquest'";
        $users = $this->ctx->db->query($sql);

        $final = $ban = array('a'=>array(),'n'=>array());
        foreach($users as $user) {
            $st = $user['state'];
            if($st !== null && $st != Model_Quest::STATE_INBAN) {
                $final[$st==Model_Quest::STATE_INFINAL?"a":"n"][] = $user;
            }
            $ban[$st==Model_Quest::STATE_INBAN?"a":"n"][] = $user;
        }
        $this->view->data['final'] = $final;
        $this->view->data['ban']   = $ban;

        $this->rh->response->setContent($this->view);
    }

    private function update(){

        //убрать из группы
        $state = $this->rh->POST['update']=="final"?
                Model_Quest::STATE_INQUEST:null;
        foreach($this->rh->POST['a_list'] as $iid) {
            $this->model->setDocumentState($this->oQuest->id, $iid, $state);
        }
        //добавить в группу
        $state = $this->rh->POST['update']=="final"?
                Model_Quest::STATE_INFINAL:Model_Quest::STATE_INBAN;
        foreach($this->rh->POST['n_list'] as $iid) {
            $this->model->setDocumentState($this->oQuest->id, $iid, $state);
        }
    }
}
